<script setup lang="ts">
import { useFormRules, useNaiveForm } from '@/hooks/common/form';
import { ipsecEnableOptions } from '@/constants/business';
import { translateOptions } from '@/utils/common';

defineOptions({
  name: 'VPNSearch'
});

interface Emits {
  (e: 'reset'): void;
  (e: 'search'): void;
}

const emit = defineEmits<Emits>();

const { formRef, validate, restoreValidation } = useNaiveForm();

const model = defineModel<TicketManageApi.VPNManage.VPNSearchParams>('model', { required: true });

type RuleKey = keyof Pick<TicketManageApi.VPNManage.VPN, 'name'>;

const { createLengthRule } = useFormRules();

const rules: Record<RuleKey, CommonType.WithArray<App.Global.FormRule>> = {
  name: [createLengthRule(64)]
};

async function reset() {
  await restoreValidation();
  emit('reset');
}

async function search() {
  await validate();
  emit('search');
}
</script>

<template>
  <NCard :bordered="false" size="small" class="card-wrapper">
    <NCollapse>
      <NCollapseItem title="搜索" name="user-search">
        <NForm
          ref="formRef"
          :model="model"
          :rules="rules"
          label-placement="left"
          :label-width="120"
          @keydown.enter="search"
        >
          <NGrid responsive="screen" item-responsive>
            <NFormItemGi span="24 s:12 m:6" label="配置名称" path="name" class="pr-24px">
              <NInput v-model:value="model.name" placeholder="请输入配置名称" />
            </NFormItemGi>
            <NFormItemGi span="24 s:12 m:6" label="隧道是否启用" path="enable" class="pr-24px">
              <BooleanSelect
                v-model:value="model.enable"
                placeholder="请选择隧道是否启用"
                :options="translateOptions(ipsecEnableOptions)"
                clearable
              />
            </NFormItemGi>
            <NFormItemGi span="24 m:12">
              <NSpace class="w-full" justify="end">
                <NButton @click="reset">
                  <template #icon>
                    <icon-local-ic-round-refresh class="text-icon" />
                  </template>
                  重置
                </NButton>
                <NButton type="primary" ghost @click="search">
                  <template #icon>
                    <icon-local-ic-round-search class="text-icon" />
                  </template>
                  搜索
                </NButton>
              </NSpace>
            </NFormItemGi>
          </NGrid>
        </NForm>
      </NCollapseItem>
    </NCollapse>
  </NCard>
</template>

<style scoped></style>
